﻿<%@ WebHandler Language="C#" Class="infoPublish" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;

public class infoPublish : AjaxHandlerBase{

    public infoPublish()
    {
        //发布公共信息
        htTypePermit.Add("publish", Constants.Permit_PublishPublicInfo);
        //文件上传
        htTypePermit.Add("fileupload", Constants.Permit_PublishPublicInfo);
        //删除公共信息
        htTypePermit.Add("del", Constants.Permit_DelPublicInfo);
    }
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strRet = "";
        string strOp = context.Request.Params["op"];
        try
        {
            switch (strOp)
            {
                case "fileupload":
                    Fileupload(context,Constants.FilePath_PublicInfo);
                    return;
                    break;
                case "publish":
                    strRet = publishInfo(context);
                    break;
                case "del":
                    strRet = delInfo(context);
                    break;
                case "getInfoType":
                    strRet = getInfoType(context);
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            context.Response.Write(strRet);
        }
    }

    //发布信息
    private string publishInfo(HttpContext context)
    {
        string strRet;
        string strMsg;

        string strModel = context.Request.Params["model"];
        string strFiles = context.Request.Params["filenames"];
        string strSaveFiles = context.Request.Params["savenames"];

        string strPath = Constants.FilePath_PublicInfo;
        
        Chenxin.Lib.Hr.Entity.Pblish_Info entInfo =
        (Chenxin.Lib.Hr.Entity.Pblish_Info)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(strModel, typeof(Chenxin.Lib.Hr.Entity.Pblish_Info));

        entInfo.PublisherId = CurrentUserInfo.UserId;
        entInfo.PublisherName = CurrentUserInfo.UserName;
        entInfo.PublisherCode = CurrentUserInfo.UserCode;
        entInfo.DepartId = CurrentUserInfo.DepartId;
        entInfo.DepartName = CurrentUserInfo.DepartName;
        entInfo.CompanyId = CurrentUserInfo.CompanyId;
        entInfo.CompanyName = CurrentUserInfo.CompanyName;
        entInfo.CityId = CurrentUserInfo.CityId;
        entInfo.CityName = CurrentUserInfo.CityName;
        
        //添加
        if (entInfo.RecordId.Equals(Guid.Empty))
        {
            entInfo.RecordId = Guid.NewGuid();
            entInfo.IsDel = false;
            entInfo.Addtime = DateTime.Now;
            //发布信息
            Chenxin.Lib.Hr.BLL.Pblish_Info.Add(entInfo);
            //附件
            if (!string.IsNullOrEmpty(strFiles))
            {
                strPath = strPath + strSaveFiles;
                AddAttachments(entInfo.RecordId, strPath, strFiles, 30);
            }

            strMsg = "发布成功";
        }
        //编辑
        else
        {
            string uploadfl = context.Request.Params["uploadfl"];
            //原信息
            Chenxin.Lib.Hr.Entity.Pblish_Info entOldInfo = Chenxin.Lib.Hr.BLL.Pblish_Info.GetModel(entInfo.RecordId);
            entInfo.IsDel = entOldInfo.IsDel;
            entInfo.Addtime = entOldInfo.Addtime;
            
            //发件箱
            Chenxin.Lib.Hr.BLL.Pblish_Info.Update(entInfo);

            //删除原有文件
            if (!string.IsNullOrEmpty(uploadfl))
            {
                DelAttachments(context, new Guid(uploadfl));
            }
            //附件
            if (!string.IsNullOrEmpty(strFiles))
            {
                strPath = strPath + strSaveFiles;
                AddAttachments(entInfo.RecordId, strPath, strFiles, 30);
            }

            strMsg = "修改成功";
        }

        strRet = retExecResult(Constants.ExecResult.Succ, strMsg, "");
        return strRet;
    }

    //删除信息
    private string delInfo(HttpContext context)
    {
        string strRet;

        string RecordId = context.Request.Params["RecordId"];
        //删除信息
        Chenxin.Lib.Hr.BLL.Pblish_Info.Delete(new Guid(RecordId));

        //删除附件
        System.Data.DataSet ds = Chenxin.Lib.Hr.BLL.Attachment.GetListByBelong(RecordId, 30);
        if (ds.Tables[0].Rows.Count > 0)
        {
            DelAttachments(context, new Guid(ds.Tables[0].Rows[0]["RecordId"].ToString()));
        }
        //删除附件表
        Chenxin.Lib.Hr.BLL.Attachment.DeleteByReportId(new Guid(RecordId));
        
        strRet = retExecResult(Constants.ExecResult.Succ, "删除成功", "");
        return strRet;
    }
    
    //获得信息类型
    private string getInfoType(HttpContext context)
    {
        string strRet;

        //生成JSON字符串
        System.Text.StringBuilder strHrml = new System.Text.StringBuilder();

        for (int idxCnt = 0; idxCnt < Constants.PublicInfoType.Length; idxCnt++)
        {
            if (CurrentUserInfo.ContainsPermit(Constants.PublicInfoType[idxCnt][1]))
            {
                strHrml.Append("{ txt: '" + Constants.PublicInfoType[idxCnt][0] + "', val: '" + Constants.PublicInfoType[idxCnt][2] + "' },");
            }
        }
        if (strHrml.Length > 0)
        {
            strHrml.Remove(strHrml.Length - 1, 1);
        }
        strRet = string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", strHrml.ToString());
        return strRet;
    }
}